using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectVideo()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_VIDEO
			};
			return View(CreateViewModel(data));
		}

		public ActionResult SelectVideoPaging(int page = 1)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_VIDEO,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListVideo", CreateViewModel(data));
		}

		public ActionResult SelectVideoByIDAlbum(int idalbum, int page = 1)
		{
			var data = new DataTransferViewModel
			{
				IDAlbum = idalbum,
				EnumViewModelType = EnumViewModel.ADMIN_VIDEO,
				CurrentPage = page
			};
			return View("SelectVideo", CreateViewModel(data));
		}

		public ActionResult SelectVideoByIDAlbumPaging(int idalbum, int page = 1)
		{
			var data = new DataTransferViewModel
			{
				IDAlbum = idalbum,
				EnumViewModelType = EnumViewModel.ADMIN_VIDEO,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListVideo", CreateViewModel(data));
		}


		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeleteVideo(int id)
		{
			var deleteItem = _repVideo.SelectByID(id);
			var fileName = deleteItem.HinhMinhHoa;
			var idalbum = deleteItem.IDAlbum;
			var result = _repVideo.Delete(id);
			if (result)
			{
				if (fileName != "noimages.jpg")
				{
					var fullPath = Server.MapPath("/Content/Images/Items/" + fileName);
                    FileHelper.DeleteFile(fullPath);
				}
			}
			return Json(new{
				Success = result,
				RecordCount = _repVideo.SelectAll().Count,
				DeleteId = id
			});
		}

		public ActionResult DetailOfVideo(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_VIDEO,
				IntID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateVideo(FormCollection forms)
		{
			string id = forms.GetValues("Video_ID")[0];
			string idalbum = forms.GetValues("Video_IDAlbum")[0];
			string tenvideo = forms.GetValues("Video_TenVideo")[0];
			string url = forms.GetValues("Video_URL")[0];
			var file = Request.Files["Video_HinhMinhHoa"];
			string refName = "";
			var editItem = _repVideo.SelectByID(int.Parse(id));
			editItem.IDAlbum = int.Parse(idalbum.Replace(".", "").Replace(",", ""));
			editItem.TenVideo = tenvideo;
			editItem.URL = url;
			var result = false;
			if (file.ContentLength != 0)
			{
                result = FileHelper.UploadFile(file, Server.MapPath("/Content/Images/Items/"), id, editItem.HinhMinhHoa, WebConfiguration.SmallImageWidth, WebConfiguration.SmallImageHeight, ref refName);
				editItem.HinhMinhHoa = refName;
			}
			result = _repVideo.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_VIDEO,
				IntID = int.Parse(id.Replace(".", "").Replace(",", "")),
				InfoText = result ? "Item has been updated" : "Cannot update this item"
			};
			return View("DetailOfVideo", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertVideo(FormCollection forms)
		{
			string idalbum = forms.GetValues("Video_IDAlbum")[0];
			string tenvideo = forms.GetValues("Video_TenVideo")[0];
			string url = forms.GetValues("Video_URL")[0];
			var file = Request.Files["Video_HinhMinhHoa"];
			string refName = "";
			var newItem = new Video
			{
				IDAlbum = int.Parse(idalbum),
				TenVideo = tenvideo,
				URL = url,
				HinhMinhHoa = "noimages.jpg"
			};
			var result = _repVideo.Insert(newItem);
			if (result)
			{
				if (file.ContentLength != 0)
				{
                    result = FileHelper.UploadFile(file, Server.MapPath("/Content/Images/Items/"), newItem.ID.ToString(), string.Empty, WebConfiguration.SmallImageWidth, WebConfiguration.SmallImageHeight, ref refName);
					if (result)
					{
						var addedItem = _repVideo.SelectByID(newItem.ID);
						addedItem.HinhMinhHoa = refName;
						_repVideo.Save();
					}
				}
			}
			var data = new DataTransferViewModel
			{
				IDAlbum = int.Parse(idalbum),
				EnumViewModelType = EnumViewModel.ADMIN_VIDEO,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectVideo", CreateViewModel(data));
		}


	}
}